/*
 * Copyright (C) 2012 Moravská zemská knihovna v Brně / Moravian Library in Brno
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mzk.oai.provider;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class OaiRequestParameters {

    private static final Logger logger = Logger.getLogger(OaiRequestParameters.class.getName());
    private final Map<String, String[]> map = new HashMap<String, String[]>();

    OaiRequestParameters(HttpServletRequest request) throws UnsupportedEncodingException {
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String[] values = request.getParameterValues(name);
            String[] decodedValues = new String[values.length];

            StringBuilder valuesString = new StringBuilder();
            StringBuilder decodedValuesString = new StringBuilder();
            valuesString.append("[");
            decodedValuesString.append("[");
            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                String urlDecoded = URLDecoder.decode(value, "UTF-8");
                decodedValues[i] = urlDecoded;

                valuesString.append(value);
                decodedValuesString.append(urlDecoded);
                if (i != values.length - 1) {
                    valuesString.append(", ");
                    decodedValuesString.append(", ");
                }
            }
            map.put(name, decodedValues);
            valuesString.append("]");
            decodedValuesString.append("]");
            logger.log(Level.FINE, "{0}: recieved={1}, decoded={2}", new Object[]{name, valuesString, decodedValuesString});
        }
    }

    Map<String, String[]> getMap() {
        return map;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            //key:value(s) separator
            String key = iterator.next();
            result.append(key).append(": ");

            //values opening bracket
            String[] values = map.get(key);
            if (values.length > 1) {
                result.append('[');
            }

            //value(s)
            for (int i = 0; i < values.length; i++) {
                result.append(values[i]);
                if (i != values.length - 1) {
                    result.append(',');
                }
            }

            //values closing bracket
            if (values.length > 1) {
                result.append(']');
            }

            //parameter record separator
            if (iterator.hasNext()) {
                result.append(", ");
            }
        }
        return result.toString();
    }
}
